package com.chuck.shirodemo.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;

/**
 *自定义数据源
 */
public class CustomRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //假设数据库中获取出来的用户名和密码是
        String username = "chuck";
        String password = "123456";

        /*//判断用户名
        UsernamePasswordToken token= (UsernamePasswordToken)authenticationToken;
        String usernameInput = token.getUsername();*/

        String usernameInput =(String)authenticationToken.getPrincipal();
        if(!usernameInput.equals(username)){
            throw new UnknownAccountException("用户名不存在");
        }

        //判断密码
        String passwordInput =new String ((char[]) authenticationToken.getCredentials()) ;
        if (!passwordInput.equals(password)){
            throw new IncorrectCredentialsException("密码不正确");
        }

        //拿到认证信息
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, this.getName());

        return info;
    }

}
